#include "sqlite/singledb.h"


SingleDB*SingleDB::db=nullptr;
SingleDB *SingleDB::getInstance()
{
    if(SingleDB::db==nullptr)
    {
        SingleDB::db=new SingleDB();
    }
    return SingleDB::db;
}

SingleDB::SingleDB()
{
    openDataBase("../database/VatulDB.db");
}




int SingleDB::openDataBase(char *path)
{
    int res=sqlite3_open(path,&(this->psqlite3));

    if(res==SQLITE_OK)
    {
        qDebug()<<"连接成功";
    }
    else {
        qDebug()<<"读取数据失败，错误码为"<<sqlite3_errcode(psqlite3);
    }

    return res;

}

void SingleDB::closeDataBase()
{
    int res;
    if(this->psqlite3!=nullptr)
    {
        res=sqlite3_close(psqlite3);
        if(res!=SQLITE_OK)
        {
            qDebug()<<"关闭失败";
        }
        psqlite3=nullptr;
    }
}

SingleDB::~SingleDB()
{
    closeDataBase();
}

sqlite3 *SingleDB::getPsqlite3()const
{
    return this->psqlite3;
}

void SingleDB::relaseInstance()
{
    if(SingleDB::db!=nullptr)
    {
        delete SingleDB::db;
        SingleDB::db=nullptr;
    }
}
